package com.jie.deletefile.Weather;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.jie.deletefile.R;
import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;

public class WeatherActivity extends AppCompatActivity implements View.OnClickListener {

    private TextView tv_city;
    private TextView tv_date;
    private TextView tv_week;
    private TextView weather;
    private TextView temp;
    private TextView wind;
    private TextView advice;
    private Button btn;
    private String TAG = "Weather";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather);
        initView();
//        MyAsynctask myAsynctask = new MyAsynctask();
//        myAsynctask.execute(setCity());


        OkHttpClient mokHttpClient = new OkHttpClient();
        Request request = new Request.Builder().url(setCity()).build();
        Call call = mokHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onResponse(Response response) {
//                Log.e(TAG, "onResponse: "+response.body().string() );
                String string = null;
                try {
                    string = response.body().string();
                } catch (IOException e) {
                    e.printStackTrace();
                } finally {

                }
                Entity.ResultBean.TodayBean todayBean =  parser.gettodayData(string);
                Message msg = handler.obtainMessage();
                msg.obj = todayBean;
                handler.sendMessage(msg);
            }
        });

    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Entity.ResultBean.TodayBean todayBean = (Entity.ResultBean.TodayBean) msg.obj;
            tv_date.setText(todayBean.getDate_y());
            tv_week.setText(todayBean.getWeek());
            tv_city.setText(todayBean.getCity());
            weather.setText(todayBean.getWeather());
            wind.setText(todayBean.getWind());
            advice.setText(todayBean.getDressing_advice());
            temp.setText(todayBean.getTemperature());
        }
    };

    private String setCity() {
        Intent intent = getIntent();
        String city = intent.getStringExtra("city");
        if (city == null) {
            city = "南京";
        }
        String uri = "http://v.juhe.cn/weather/index?format=2&cityname=" + city + "&key=3a9910e6ed7e31acdad1f6c38a9b5b2f";
        return uri;
    }

    public void change(View view) {
        Intent intent = new Intent(WeatherActivity.this, CityActivity.class);
        startActivity(intent);
    }

    private void initView() {
        tv_city = (TextView) findViewById(R.id.tv_city);
        tv_date = (TextView) findViewById(R.id.tv_date);
        tv_week = (TextView) findViewById(R.id.tv_week);
        weather = (TextView) findViewById(R.id.weather);
        temp = (TextView) findViewById(R.id.temp);
        wind = (TextView) findViewById(R.id.wind);
        advice = (TextView) findViewById(R.id.advice);
        btn = (Button) findViewById(R.id.btn);

        btn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn:

                break;
        }
    }
    //请求数据失败

//    class MyAsynctask extends AsyncTask<String, Void, String> {
//
//        @Override
//        protected String doInBackground(String... params) {
//
//
//            StringBuffer buffer = new StringBuffer();
//            BufferedReader reader = null;
//            try {
//                URL url = new URL(params[0]);
//                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
//                conn.setReadTimeout(3000);
//                conn.setConnectTimeout(3000);
//                conn.connect();
//                if (conn.getResponseCode() == 200) {
//                    InputStream is = conn.getInputStream();
//                    InputStreamReader inputStreamReader = new InputStreamReader(is);
//                    reader = new BufferedReader(inputStreamReader);
//
//                    String s = null;
//                    while ((s = reader.readLine()) != null) {
//                        buffer.append(s);
//                    }
//
//                } else
//                    Log.e(TAG, "doInBackground: 1111111111111111111111");
//            } catch (IOException e) {
//                e.printStackTrace();
//            } finally {
//                if (reader != null) {
//                    try {
//                        reader.close();
//                    } catch (IOException e) {
//                        e.printStackTrace();
//                    }
//
//                }
//
//            }
//            Log.e(TAG, "doInBackground: " + buffer.toString());
//            return buffer.toString();
//        }
//
//        @Override
//        protected void onPostExecute(String s) {
//            super.onPostExecute(s);
//            Entity.ResultBean.TodayBean todayBean = parser.gettodayData(s);
//            Log.e(TAG, "onPostExecute: " + todayBean.getDate_y());
//            tv_date.setText(todayBean.getDate_y());
//            tv_week.setText(todayBean.getWeek());
//            tv_city.setText(todayBean.getCity());
//            weather.setText(todayBean.getWeather());
//            wind.setText(todayBean.getWind());
//            advice.setText(todayBean.getDressing_advice());
//            temp.setText(todayBean.getTemperature());
//
//        }
//    }
}
